<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/13/013
 * Time: 9:16
 */

namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;

class Project extends Controller
{

    /**
     * 筛选条件
     */
    public function screenApi(){
        $progress = [['value'=>1,'title'=>"正常"],['value'=>2,'title'=>"逾期"]];
        $comment = [['value'=>1,'title'=>"已点评"],['value'=>2,'title'=>"未点评"]];
        $pro_type = $this->dictionary("pro_type",$_POST['appid']);
        $type = [];
        foreach ($pro_type as $v){
            $type[] = $v;
        }
        $black['progress'] = $progress;
        $black['comment'] = $comment;
        $black['type'] = $type;
        return json_encode($black);
    }


    /**
     * 数据字典，查询字典
     * @param $keword
     * @return mixed
     */
    public function dictionary($keword,$appid){
        $config = Db::name("config")->where(['xcx_appid' => $appid])->find();
        $where['uniacid'] = $config['id'];
        $black = Db::name("project_config")->where($where)->value($keword);
        $black = explode("|","$black");
        $data = [];
        foreach ($black as $k=>$v){
            $v = explode(":","$v");
            $data[] = ['value'=>$v[0],'title'=>$v[1]];
        }
        return $data;
    }


    /**
     * 查询我负责的项目
     * @param $name
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function conscientious($name){
        $where = "(duty_person like '%".$name."%') and status=1";
        $pro = Db::name("project_pro")->where($where)->field("id")->select();
        $pro_v = empty($pro)?0:array_column($pro,'id');
        return $pro_v;
    }


    /**
     * 项目投资金额
     */
    public function investment(){

        try{
            if(empty($_POST['appid'])){throw new Exception("appid不能为空");}
            $appid = $_POST['appid'];
            $config = Db::name("config")->where(['xcx_appid' => $appid])->find();

            //当前年
            $year = empty($_POST['year'])?date("Y",time()):$_POST['year'];
            //查询本年总投资
            $where['onstream_time'] = ['like',"%{$year}%"];
            $where['status'] = 1;
            $where['uniacid'] = $config['id'];
            $investment = Db::name("project_pro")->where($where)->sum("investment");
            $investment = ($investment*10000)/10000000;

            //本年度计划投资
            $where1['a.plan_title'] = $year;
            $where1['b.status'] = 1;
            $where1['b.uniacid'] = $config['id'];
            $plan_investment = Db::name("project_pro_plan")->alias('a')
                ->join("project_pro b","a.pro_id = b.id")->where($where1)->sum("plan_investment");
            $plan_investment = ($plan_investment*10000)/10000000;

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['investment'] = $investment;
            $black['plan_investment'] = $plan_investment;
//            $black['procount'] = $procount;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 项目投资数量
     */
    public function projectCount(){

        try{
            if(empty($_POST['appid'])){throw new Exception("appid不能为空");}
            $appid = $_POST['appid'];
            $config = Db::name("config")->where(['xcx_appid' => $appid])->find();

            //当前年
            $year = empty($_POST['year'])?date("Y",time()):$_POST['year'];
            //项目数量
            $typeValue = [];

            $where['onstream_time'] = ['like',"%{$year}%"];
            $where['status'] = 1;
            $where['uniacid'] = $config['id'];
            $procount = Db::name("project_pro")->where($where)->count("*");
            $typeValue[] = ['name'=>'年度总项目','value'=>$procount];

            //查询字典
            $pro_type = $this->dictionary("pro_type",$appid);
            $harmonious = 0;
            foreach ($pro_type as $k=>$v){
                $where['project_type'] = $v['value'];
                $pro_type[$k]['count'] = $count = Db::name("project_pro")->where($where)->count("*");
                $harmonious = $harmonious+$count;
                $typeValue[] = ['name'=>$v['title'],'value'=>$count];
            }
            $other = $procount - $harmonious;
            $typeValue[] = ['name'=>'其他项目','value'=>$other];

            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['typeValue'] = $typeValue;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }


    /**
     * 关联项目列表
     */
    public function listApi(){
        $common = new Common();
        $project_user = $common->testusername();
//        $project_user = Session::get("project_user");
        try{
            //判断是否有点评权限
            $commentAuto = Db::name("project_account")->where(['id'=>$project_user['id']])->value("comment");

            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $limit = empty($_POST['limit'])?10:intval($_POST['limit']);
            $start = ($page - 1) * $limit;

            if(empty($project_user)){throw new Exception("请先登录");}
            //已关联项目
            $proWhere['account_id'] = $project_user['id'];
            $project_id = Db::name("project_account_pro")->where($proWhere)->field("project_id")->select();
            $project_id = array_column($project_id,'project_id');
            $project_ids = implode($project_id,",");
            if(empty($project_ids)){
                $project_ids = [0];
            }
            //已关联项目end

            // or leader like '%".$name."%' 领导人查询
            $name = $project_user['name'];
            $where = "(id in (".$project_ids.") or duty_person like '%".$name."%') and status=1";

            //筛选
            if(!empty($_POST['comment'])){
                $where .=" and comment=".$_POST['comment'];
            }
            if(!empty($_POST['project_type'])){
                $where .=" and project_type=".$_POST['project_type'];
            }
            if(!empty($_POST['keyword'])){
                $where .=" and title like '%".$_POST['keyword']."%'";
            }

            $list = Db::name("project_pro")->where($where)
                ->field("id,title,thumb,duty_person,leader,progress,year_progress")
                ->limit($start,$limit)
                ->order("id desc")->select();

            foreach ($list as $k=>$v){
                if(in_array($v['id'],$project_id) & $commentAuto==1){
                    //判断是否已点评
                    $commentWhere['pro_id'] = $v['id'];
                    $commentWhere['add_id'] = $project_user['id'];
                    $comment = Db::name("project_pro_comment")->where($commentWhere)->value("id");
                    $list[$k]['comment'] = empty($comment)?1:2;
                }

                if(strpos($v['duty_person'],$project_user['name'])!==false){

                    //是否存在点评信息
                    $commentCount = Db::name("project_pro_comment")->where(['pro_id'=>$v['id']])->count("id");
                    //已读数
                    $readCount = Db::name("project_pro_comment_log")->where(['pro_id'=>$v['id'],'read_id'=>$project_user['id']])->count("id");

                    //是否存在未读信息
                    if($commentCount!=0){
                        $list[$k]['read'] = ($commentCount!=$readCount)?1:2;
                    }
                }
            }
            $count = Db::name("project_pro")->where($where)->count("*");
            $black['code'] = 0;
            $black['msg'] = "查询成功";
            $black['list'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取项目详情
     */
    public function project_detail(){
        try{
            //查询项目基本信息
            $where['id'] = $_POST['id'];
            $detail = Db::name("project_pro")->where($where)
                ->field("id,title,thumb,duty_unit,duty_person,content,remark,onstream_time,plan_complete_time,complete_time,project_type,progress,year_progress,leader,investment")
                ->find();
            //形象进度
            $proWhere['pro_id'] = $detail['id'];
            $figure = Db::name("project_pro_figure")->where($proWhere)->field("id,title,show_time,content,images")->order("id desc")->select();
            foreach ($figure as $k=>$v){
                $figure[$k]['images'] = explode(";",$v['images']);
            }
            //推进进度
            $plan = Db::name("project_pro_plan")->where($proWhere)->field("id,plan_title,plan_investment,plan_content")->order("plan_title desc")->select();

            //点评内容
            $comment = Db::name("project_pro_comment")->where($proWhere)->field("id,content,images,addtime")->order("id desc")->select();
            foreach ($comment as $k=>$v){
                $comment[$k]['images'] = explode(";",$v['images']);
                $comment[$k]['title'] = $detail['title'];
                $comment[$k]['duty_unit'] = $detail['duty_unit'];
            }
            $black['detail'] = $detail;
            $black['figure'] = $figure;
            $black['plan'] = $plan;
            $black['comment'] = $comment;
            $black['code'] = 0;
            $black['msg'] = "查询成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 点评操作
     */
    public function commentApi(){
        $common = new Common();
        $project_user = $common->testusername();
//        $project_user = Session::get("project_user");
        try{
            if(empty($project_user)){throw new Exception("请先登录");}
            //判断是否有点评权限
            $comment = Db::name("project_account")->where(['id'=>$project_user['id']])->value("comment");
            if($comment!=1){
                throw new Exception("暂无点评权限");
            }
            $data['pro_id'] = $_POST['pro_id'];
//            $data['title'] = $_POST['title'];
            $data['content'] = $_POST['content'];

            if(!empty($_POST['images'])){
                $images = $_POST['images'];
                $data['images'] = implode(";",$images);
            }

            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = $project_user['id'];
            $res = Db::name("project_pro_comment")->insertGetId($data);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 点评列表
     */
    public function commentlistApi(){
        $common = new Common();
        $project_user = $common->testusername();
        try{
            //查询我负责的项目
            $name = $project_user['name'];
            $read = empty($_POST['read'])?1:$_POST['read'];
            // or leader like '%".$name."%

            $pro_v = $this->conscientious($name);

            //查询已读
            $where['pro_id'] = ['in',$pro_v];
            $where['read_id'] = $project_user['id'];
            $commentlog = Db::name("project_pro_comment_log")->where($where)->field("comment_id")->select();
            $commentlog = array_column($commentlog,"comment_id");

            if(!empty($commentlog)){
                $type = ($read==1)?"not in":"in";
                $commentWhere['a.id'] = [$type,$commentlog];
            }

            if(!empty($_POST['keyword'])){
                $commentWhere['b.name'] = ["like","%{$_POST['keyword']}%"];
            }


            //查询点评列表
            $commentWhere['a.pro_id'] = ['in',$pro_v];
            $comment = Db::name("project_pro_comment")->alias("a")
                ->join("project_account b","a.add_id = b.id")
                ->field("count(a.id) count,a.add_id,a.content,b.name,b.remark")
                ->where($commentWhere)->group('a.add_id')->select();

//            foreach ($comment as $k=>$v){
//                //查询点评人
//                $account = Db::name("project_account")->where(['id'=>$v['add_id']])->field("id,")->find();
//                $comment[$k]['name'] = $account['name'];
//            }
            $black['list'] = $comment;
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 查看点评信息
     */
    public function lookOver(){
        $common = new Common();
        $project_user = $common->testusername();

        try{
            $name = $project_user['name'];
            $add_id = $_POST['add_id'];
//            $add_id = 2;

            $pro_v = $this->conscientious($name);

            //展示评论信息
            $commentWhere['a.pro_id'] = ['in',$pro_v];
            $commentWhere['a.add_id'] = $add_id;
            $list = Db::name("project_pro_comment")->alias("a")
                ->join("project_pro b","a.pro_id = b.id")
                ->field("a.content,a.addtime,a.images,b.title,b.duty_unit,a.id,a.pro_id")->where($commentWhere)->select();


            foreach ($list as $k=>$v){
                $list[$k]['images'] = explode(';',$v['images']);
                $data = [];
                //记录查看
                $data['comment_id'] = $v['id'];
                $data['pro_id'] = $v['pro_id'];
                $data['read_id'] = $project_user['id'];

                $log = Db::name("project_pro_comment_log")->where($data)->value('id');
                if(empty($log)){
                    $data['read_time'] = date("Y-m-d H:i:s",time());
                    Db::name("project_pro_comment_log")->insert($data);
                }
                //记录查看end

            }

            $black['code'] = 0;
            $black['list'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);

    }


}